package com.likai.blog.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.likai.blog.lucene.BlogIndex;
import com.likai.blog.pojo.Blog;
import com.likai.blog.pojo.Comment;
import com.likai.blog.service.IBlogService;
import com.likai.blog.service.ICommentService;
import com.likai.blog.utils.StringUtil;

@Controller
@RequestMapping(value="/blog")
public class BlogController {
	
	@Autowired
	@Qualifier("blogService")
	private IBlogService blogService ;
	
	@Autowired
	@Qualifier("commentService")
	private ICommentService commentService ;
	
	private BlogIndex blogIndex = new BlogIndex() ;

	/**
	 * 博客详情
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/articles/{id}")
	public ModelAndView minute(
			@PathVariable("id") String id,
			HttpServletRequest request
			) {
		ModelAndView mv = new ModelAndView() ;
		Blog blog = blogService.findByBlogId(id) ;
		
		String keyWords = blog.getKeyWord() ;
		List<String> keyWordList = new ArrayList<String>() ;
		if(StringUtil.isNotEmpty(keyWords)) {
			String [] arr = keyWords.split(" ") ;
			for(int x = 0 ; x < arr.length ; x ++ ) {
				if(!" ".equals(arr[x])) {
					keyWordList.add(arr[x]) ;
				}
			}
			mv.addObject("keyWordList",keyWordList) ;
		} else {
			mv.addObject("keyWordList",null) ;
		}
		Blog lastBlog = blogService.findLastInfo(blog) ;
		
		Blog nextBlog = blogService.findNextInfo(blog) ;
		
		List<Comment> commentList = commentService.findListByBlogId(id,"1") ;
		
		mv.addObject("blog", blog) ;
		
		mv.addObject("pageCode", this.getLastAndNextCode(lastBlog, nextBlog, request.getServletContext().getContextPath())) ;
		
		mv.addObject("commentList", commentList) ;
		
		mv.addObject("mainPage", "/foreground/blog/view.jsp") ;
		
		mv.addObject("pageTitle", blog.getTitle()) ;
		
		mv.setViewName("mainTemp");
		
		return mv ;
	}
	
	/**
	 * 获取上一篇与下一篇博客
	 * @param lastBlog
	 * @param nextBlog
	 * @param projectContext
	 * @return
	 */
	private String getLastAndNextCode(Blog lastBlog,Blog nextBlog,String projectContext) {
		StringBuilder sb = new StringBuilder() ;
		if(lastBlog != null) {
			sb.append("<p>上一篇：<a href='"+ projectContext +"/blog/articles/"+ lastBlog.getBlogId() +".html'>"+ lastBlog.getTitle() +"</a></p>") ;
		} else {
			sb.append("<p>上一篇：没有了</p>") ;
		}
		
		if(nextBlog != null) {
			sb.append("<p>上一篇：<a href='"+ projectContext +"/blog/articles/"+ nextBlog.getBlogId() +".html'>"+ nextBlog.getTitle() +"</a></p>") ;
		} else {
			sb.append("<p>下一篇：没有了</p>") ;
		}
		
		return sb.toString() ;
	}
	
	/**
	 * 根据关键字全文检索博客信息
	 * @param q
	 * @param page
	 * @param pagesize
	 * @return
	 */
	@RequestMapping(value="/q")
	public ModelAndView search(
			String q,
			Integer page,
			HttpServletRequest request
			) throws Exception {
		if(page == null) {
			page = 1 ;
		}
		int pageSize = 10 ;
		ModelAndView mv = new ModelAndView() ;
		
		List<Blog> blogList = blogIndex.searchBlog(q) ;
		
		mv.addObject("pageTitle", "搜索关键字'" + q +"'结果页面") ;
		
		mv.addObject("mainPage", "foreground/blog/result.jsp") ;
		
		Integer toIndex =  blogList.size() >= page * pageSize ? page * pageSize : blogList.size() ;
		
		mv.addObject("blogList", blogList.subList((page - 1) * pageSize, toIndex)) ;
		
		mv.addObject("resultTotal", blogList.size()) ;
		
		mv.addObject("pageCode", this.getPageCode(page, blogList.size(), q, pageSize, request.getServletContext().getContextPath())) ;
		
		mv.addObject("q", q) ;
		
		mv.setViewName("mainTemp");
		return mv ;
	}
	
	/**
	 * 拼装分页条
	 * @param page
	 * @param totalNum
	 * @param q
	 * @param pageSize
	 * @param projectContext
	 * @return
	 */
	private String getPageCode(Integer page,Integer totalNum,String q,Integer pageSize,String projectContext) {
		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		StringBuilder pageCode=new StringBuilder();
		if(totalPage==0){
			return "";
		}else{
			pageCode.append("<nav>");
			pageCode.append("<ul class='pager'>");
			if(page>1){
				pageCode.append("<li><a href='"+projectContext+"/blog/q.html?page="+(page <= 0 ? 1 : page - 1 )+"&q="+q+"'>上一页</a></li>");
			}else{
				pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
			}
			if(page<totalPage){
				pageCode.append("<li><a href='"+projectContext+"/blog/q.html?page="+(page >= totalPage ? totalPage : page + 1  )+"&q="+q+"'>下一页</a></li>");
			}else{
				pageCode.append("<li class='disabled'><a href='#'>下一页</a></li>");
			}
			pageCode.append("</ul>");
			pageCode.append("</nav>");
		}
		return pageCode.toString();
	}
}
